package com.curso.login;


import com.curso.Sessions.SessionManager;

import com.curso.newUser.RegisterUser;



import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class SplashScreen extends Activity {
	ImageView mImageView;
	float scaleFactor;
	Matrix matrix;
	int displayWidth;
	int displayHeight;
	int imageWidth;
	int imageHeight;
	int oreja = 70;
	public static Activity activityInstance;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activityInstance=this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_maisn);
		mImageView = (ImageView) findViewById(R.id.iv1);
		matrix = new Matrix();
		Drawable img = mImageView.getDrawable();
		System.out.println(img.getIntrinsicWidth());
		Display display = getWindowManager().getDefaultDisplay();
		displayWidth = display.getWidth();
		displayHeight = display.getHeight();
		imageWidth = img.getIntrinsicWidth();
		imageHeight = img.getIntrinsicHeight();
		scaleFactor = (float) displayHeight / imageHeight;
		matrix.postScale(scaleFactor, scaleFactor);
		mImageView.setImageMatrix(matrix);
		((FrameLayout) findViewById(R.id.login)).setVisibility(View.GONE);
		animarLogo();
		System.out.println("Vamos a comprobar la sesion");
		Intent notification = getIntent();
		if(notification.hasExtra("description"))
			System.out.println("Splash "+notification.getExtras());
		SessionManager sessionManager = new SessionManager(this,true,notification);
		sessionManager.checkSession();
	
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	
	public void animarLogo() {
		
		final FrameLayout logo = (FrameLayout) findViewById(R.id.framelogo);
		ImageView imagenLogo = (ImageView) findViewById(R.id.logo);
		LayoutParams params = imagenLogo.getLayoutParams();
		params.width = (int) (0.4*displayWidth);
		params.height = params.width;
		//logo.setLayoutParams(params);
		Animation animacion = AnimationUtils.loadAnimation(this, R.anim.logo);
		
		logo.startAnimation(animacion);
		logo.postDelayed(new Runnable() {
			@Override
			public void run() {
				
				

				logo.setVisibility(View.GONE);
				ValueAnimator mAnimator = ValueAnimator.ofFloat(0, imageWidth
						* scaleFactor - displayWidth );
				mAnimator
						.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
							boolean animado = false;
							@Override
							public void onAnimationUpdate(
									
									ValueAnimator animation) {
								
								float value = (Float) animation
										.getAnimatedValue();
								matrix.reset();
								matrix.postScale(scaleFactor, scaleFactor);
								matrix.postTranslate(-value, 0);
								mImageView.setImageMatrix(matrix);
								if (value >= imageWidth * scaleFactor - 2*displayWidth && animado==false) {
									animado=true;
									((FrameLayout) findViewById(R.id.login)).setVisibility(View.VISIBLE);
									animarLogin();
								}
							}
						});
				mAnimator.setDuration(3000);
				mAnimator.start();
			}
		}, 2000);
		
	
	}

	public void animarLogin() {
		final FrameLayout login = (FrameLayout) findViewById(R.id.login);
		Animation animacion = AnimationUtils.loadAnimation(this, R.anim.login);
		login.startAnimation(animacion);

	}



	public void crearCuenta(View view) {
		Intent i = new Intent(SplashScreen.this, RegisterUser.class);
		startActivity(i);

	}

	public void registro(View view) {
		Intent i = new Intent(SplashScreen.this, LoginActivityCarta.class);
		startActivity(i);
		overridePendingTransition(R.anim.zoom_back_in,R.anim.zoom_back_out);
	}
	
}
